package com.chebaohui.iqs.service.impl.wxutil;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.sdk.util.RandomStringGenerator;
import com.chebaohui.iqs.sdk.util.Signature;

/**
 * 微信访问工具
 * 
 * @author xielonghai
 *
 */
public class AuthUtil {

	/**
	 * get 请求
	 * 
	 * @param url
	 * @return
	 */
	public static JSONObject doGet(String url) {
		JSONObject obj = null;
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			HttpResponse response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				String res = EntityUtils.toString(entity, "UTF-8");
				obj = JSONObject.parseObject(res);
			}
			httpGet.releaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return obj;
	}

	public static String mapToStr(Map<String, Object> map) {
		StringBuffer buffer = new StringBuffer("<xml>");
		if (null != map) {
			for (String key : map.keySet()) {
				Object val = map.get(key);
				if (null != val) {
					buffer.append("<" + key + ">");
					buffer.append(val);
					buffer.append("</" + key + ">");
				}
			}
		}
		buffer.append("</xml>");
		return buffer.toString();
	}

	public static Map<String, Object> resultAPPParment(Map<String, Object> map) {
		Map<String, Object> ma = new HashMap<String, Object>();
		ma.put("appid", map.get("appid"));
		ma.put("noncestr", RandomStringGenerator.getRandomStringByLength(32));
		ma.put("partnerid", map.get("mch_id"));
		ma.put("prepayid", map.get("prepay_id"));
		ma.put("package", "Sign=WXPay");
		ma.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000L));
		String sign = Signature.getSign(ma);
		if (null != map.get("code_url")) {
			ma.put("code_url", map.get("code_url"));
		}
		ma.put("sign", sign);
		return ma;
	}

	// 下单错误码
	public static String errorCode(Object object) {
		if (object == null) {
			return "参数为空";
		} else if ("NOAUTH".equals(object)) {
			return "商户无此接口权限";
		} else if ("NOTENOUGH".equals(object)) {
			return "余额不足";
		} else if ("ORDERPAID".equals(object)) {
			return "商户订单已支付，无需重复操作";
		} else if ("ORDERCLOSED".equals(object)) {
			return "当前订单已关闭，无法支付";
		} else if ("SYSTEMERROR".equals(object)) {
			return "系统错误";
		} else if ("APPID_NOT_EXIST".equals(object)) {
			return "参数中缺少APPID";
		} else if ("MCHID_NOT_EXIST".equals(object)) {
			return "参数中缺少MCHID";
		} else if ("APPID_MCHID_NOT_MATCH".equals(object)) {
			return "appid和mch_id不匹配";
		} else if ("LACK_PARAMS".equals(object)) {
			return "缺少必要的请求参数";
		} else if ("OUT_TRADE_NO_USED".equals(object)) {
			return "同一笔交易不能多次提交";
		} else if ("SIGNERROR".equals(object)) {
			return "参数签名结果不正确";
		} else if ("XML_FORMAT_ERROR".equals(object)) {
			return "XML格式错误";
		} else if ("REQUIRE_POST_METHOD".equals(object)) {
			return "未使用post传递参数";
		} else if ("POST_DATA_EMPTY".equals(object)) {
			return "post数据不能为空";
		} else if ("NOT_UTF8".equals(object)) {
			return "请使用NOT_UTF8编码格式";
		}
		return "参数错误";
	}

}
